package com.wjj.happytime.fragment.listen_frg;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.test.mock.MockApplication;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiongbull.jlog.JLog;
import com.wjj.happytime.R;
import com.wjj.happytime.activity.listen_activity.NomalRadioListActivity;
import com.wjj.happytime.adapter.listen_frg_adapter.NomalRadioAdapter;
import com.wjj.happytime.application.BaseApplication;
import com.wjj.happytime.entity.listen_entity.ListenRadios;
import com.wjj.happytime.entity.listen_entity.RadioDetail;
import com.wjj.happytime.my_interface.HttpService;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProvinceRadioFrg extends Fragment implements Callback<ListenRadios> {

  private int provinceCode;
  public RecyclerView rvProvince;
  public HttpService service;

  public int pageNum=1;
  public int pageSize=20;
  public int radioType=2;
  public NomalRadioAdapter adapter;
  public int total;
  public boolean isLoading;
  public boolean isFrist;

  public ProvinceRadioFrg() {
    // Required empty public constructor
  }

  public ProvinceRadioFrg(int provinceCode) {
    this.provinceCode = provinceCode;
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_province_radio_frg, container, false);
  }

  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    rvProvince = ((RecyclerView) view.findViewById(R.id.rv_province));
    //数据源
    service = ((BaseApplication) getActivity().getApplication()).getService();
    service.getRadioData(pageNum,pageSize,provinceCode,radioType).enqueue(this);

    rvProvince.addOnScrollListener(new OnScrollListener() {
      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        LinearLayoutManager layout = (LinearLayoutManager) recyclerView.getLayoutManager();
        //总条目
        int totalCount = layout.getItemCount();
        //最后一个可见的条目位置
        int lastVisibleItemPosition = layout.findLastVisibleItemPosition();
        if (!isLoading&&lastVisibleItemPosition+1==totalCount&&totalCount<total) {
          isLoading=true;
          service.getRadioData(++pageNum,pageSize,provinceCode,radioType).enqueue(ProvinceRadioFrg.this);
        }
      }
    });
  }

  @Override
  public void onResponse(Call<ListenRadios> call, Response<ListenRadios> response) {
    JLog.e("http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum="+pageNum+"&pageSize=20&provinceCode="+provinceCode+"&radioType="+radioType);
    List<RadioDetail> result = response.body().getResult();
    isLoading=false;
    if (!isFrist) {
      isFrist=true;
      total = response.body().getTotal();
      JLog.e(total+"");
      adapter = new NomalRadioAdapter(getContext(),result);
      rvProvince.setAdapter(adapter);
    }else {
      adapter.addDatas(result);
    }


  }

  @Override
  public void onFailure(Call<ListenRadios> call, Throwable t) {
    isLoading=false;
  }
}
